<?php

namespace App\Admin\Controllers;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use App\Models\Requirement;
use App\Models\AdminUserRequirement;
use App\Models\AdminUserLog;
use App\Models\AdminUserPlan;
use Illuminate\Support\MessageBag;

class AdminUserPlanController extends Form
{
    /**
     * The form title.
     *
     * @var  string
     */
    public $title = '每日计划上报';

    /**
     * Handle the form request.
     *
     * @param  Request $request
     *
     * @return  \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $data = $request->all();
        $admin_user_id = Admin::user()->id;
        $admin_user_requirements = (new AdminUserRequirement())->where('admin_user_id',$admin_user_id)->whereIn('status',['未开始','进行中','停滞'])->with('requirement','requirement.module', 'requirement.module.project')->get();
        foreach($admin_user_requirements as $k=>$admin_user_requirement)
        {
            $already_plan = (new AdminUserPlan())->where([
                ['admin_user_requirement_id',$admin_user_requirement->id],
                ['date',date("Ymd",time())]
            ])->first();
            if(empty($already_plan)){
                $admin_plan = new AdminUserPlan();
            }else{
                $admin_plan = $already_plan;
            }
            
            $admin_plan->date = date("Ymd",time());
            $admin_plan->admin_user_id = $admin_user_id;
            $admin_plan->admin_user_requirement_id =$admin_user_requirement->id;
            if(isset($data['description'.$admin_user_requirement->id])){
                $admin_plan->description = $data['description'.$admin_user_requirement->id];
            }
            if(isset($data['times'.$admin_user_requirement->id])){
                $admin_plan->times =$data['times'.$admin_user_requirement->id];
            }
          
            $admin_plan->save();

        }
        
        admin_success('计划更新成功');

        return redirect('/admin/plans');
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $admin_user_id = Admin::user()->id;
        $admin_user_requirements = (new AdminUserRequirement())->where('admin_user_id',$admin_user_id)->whereIn('status',['未开始','进行中','停滞'])->with('requirement','requirement.module', 'requirement.module.project')->get();

        foreach($admin_user_requirements as $k=>$admin_user_requirement)
        {
            $this->html('<hr>');
            $this->html('<a href="/admin/admin_user_requirement/'.$admin_user_requirement->id.'" target="_blank" >'.$admin_user_requirement->requirement->module->project->name.'-'.$admin_user_requirement->requirement->module->name.'-'.$admin_user_requirement->requirement->name.'</a>','任务<b>'.($k+1).'</b>');
            
            $last_log ='';
            $last_log_stcs = (new AdminUserLog)->where('admin_user_requirement_id',$admin_user_requirement->id)->orderBy('date','DESC')->get();

            if(!$last_log_stcs->isEmpty()){
                $last_log .= <<<EOF
                    <table style="border: 1px solid black; border-collapse: collapse;">
                    <tr>
                        <th style="border: 1px solid black;padding: 5px;">日期</th>
                        <th style="border: 1px solid black;padding: 5px;">任务状态</th>
                        <th style="border: 1px solid black;padding: 5px;">计划交付日期</th>
                        <th style="border: 1px solid black;padding: 5px;">停滞原因</th>
                        <th style="border: 1px solid black;padding: 5px;">未开始原因</th>
                        <th style="border: 1px solid black;padding: 5px;">当日耗时</th>
                        <th style="border: 1px solid black;padding: 5px;">预计交付状态</th>
                        <th style="border: 1px solid black;padding: 5px;">已完成比例</th>
                        <th style="border: 1px solid black;padding: 5px;">当日工作内容描述</th>
                    </tr>
                EOF;
                foreach($last_log_stcs as $last_log_stc){
                    $last_log .= "<tr>";
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->date."</td>";//日期
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->status."</td>";//任务状态
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->plan_date."</td>";//计划交付日期
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->stop_reason."</td>";//停滞原因
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->not_start_reason."</td>";//未开始原因
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->times."</td>";//当日耗时
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->give_status."</td>";//交付状态
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->persentage."</td>";//已完成比例
                    $last_log .= "<td style='border: 1px solid black;padding: 5px;'>".$last_log_stc->description."</td>";//工作内容描述                    
                    // ，任务状态：".$last_log_stc->status.",交付状态：".$last_log_stc->give_status."，耗时：".$last_log_stc->time.($last_log_stc->status!='已完成'?("，已完成比例：".$last_log_stc->persentage."%"):"，已完成比例：100%")."，工作描述：".$last_log_stc->description;
                    
                    $last_log .= "</tr>";
                }
                $last_log .= <<<EOF
                </table>
            EOF;
                $this->html($last_log,'任务记录');
            }else{
                $this->html('空','任务记录');
            }
            $already_plan = (new AdminUserPlan())->where([
                ['admin_user_requirement_id',$admin_user_requirement->id],
                ['date',date("Ymd",time())]
            ])->first();

            if(empty($already_plan)){
                $this->text('description'.$admin_user_requirement->id,'当日工作内容描述');
                $this->text('times'.$admin_user_requirement->id,'今日预计耗时(小时）')->setWidth(2, 2)->default(0);
            }else{
                $this->text('description'.$admin_user_requirement->id,'当日工作内容描述')->default($already_plan->description);
                $this->text('times'.$admin_user_requirement->id,'今日预计耗时(小时）')->setWidth(2, 2)->default($already_plan->times);
            }
            


        }
    }

    /**
     * The data of the form.
     *
     * @return  array $data
     */
    public function data()
    {

        return [
            'name'       => 'John Doe',
            'email'      => 'John.Doe@gmail.com',
            'created_at' => now(),
        ];
    }
}